package com.jof.framework.hmc.converter;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import com.jof.framework.hmc.domain.SMSMessage;
import com.thoughtworks.xstream.XStream;

/**
 * @author zhangtao
 * 消息转换类：短信
 */
public class SMSMessageConverter implements MessageConverter {

	/**
	 * XML解析工具
	 */
	private XStream xstream;

	/* 
	 * 转换：从Message到短信
	 */
	public SMSMessage fromMessage(Message message) throws JMSException, MessageConversionException {
		xstream.processAnnotations(SMSMessage.class);
		TextMessage textMessage = (TextMessage) message;
		SMSMessage smsMessage = (SMSMessage) xstream.fromXML(textMessage.toString());
		return smsMessage;
	}

	/* 
	 * 转换：从短信到Message
	 */
	public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException {
		xstream.processAnnotations(SMSMessage.class);
		TextMessage textMessage = session.createTextMessage();
		SMSMessage smsMessage = (SMSMessage) obj;
		textMessage.setText(xstream.toXML(smsMessage));
		return textMessage;
	}

	public void setXstream(XStream xstream) {
		this.xstream = xstream;
	}

}
